﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Sell
namespace backoffice.Data
{
    partial class Sell
    {
        public void setSell(Sell s)
        {
            this.SellID = s.SellID;
            this.UserID = s.UserID;
            this.RegionID = s.RegionID;
            this.MaterialID = s.MaterialID;
            this.Price = s.Price;
            this.Currency = s.Currency;
            this.Date = s.Date;
        }
    }
}
#endregion

#region dataSell
namespace backoffice.Data
{
    public class dataSell
    {
        #region Create
        public static void createSell(Sell s)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                s.SellID = Guid.NewGuid();
                s.Date = DateTime.Now;

                db.Sells.InsertOnSubmit(s);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Sell getSell(Guid guid)
        {
            try
            {
                if (guid == null)
                {
                    return null;
                }
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    return db.Sells.FirstOrDefault(sell => sell.SellID == guid);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Sell> getSells(Expression<Func<Sell, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Sells.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeSell(Sell s)
        {
            try
            {
                if (s == null)
                {
                    return false;
                }
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Sell res = db.Sells.FirstOrDefault(sell => sell.SellID == s.SellID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        res.setSell(s);

                        db.SubmitChanges();

                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteSell(Sell s)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Sells.Contains(s))
                {
                    Sell res = db.Sells.FirstOrDefault(sell => sell.SellID == s.SellID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        db.Sells.DeleteOnSubmit(res);
                        db.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion